<?php


class Dienthoai extends Model
{
    public function getDienthoai($id)
    {
        $data = $this->Db->select('dienthoai', 'dt_id = :id', array('id' => $id));

        if (count($data) > 0) {
            return Dienthoai::format($data[0]);
        } else {
            return false;
        }
    }

    public static function getList($key)
    {
        $key = "%{$key}%";
        $data = self::$_database->run(
            'SELECT d.dt_id, dt_ten, dt_gia, h.hsx_id, hsx_ten FROM dienthoai d NATURAL JOIN hangsanxuat h' .
            ' WHERE (dt_in_use = 1 AND hsx_in_use = 1) AND (d.dt_id LIKE :key OR dt_ten LIKE :key OR h.hsx_id LIKE :key OR hsx_ten LIKE :key)',
            array('key' => $key));

        $data = self::format($data, true);

        return $data;
    }

    public static function getPhoneInfo($id)
    {
        $dt = new Dienthoai();
        return $dt->getDienthoai($id);
    }

    public function getDienthoaiFirstImage($id)
    {
        $data = $this->Db->select('hinhdienthoai','dt_id = :id', array('id' => $id));
        if(count($data) > 0)
        {
            return $data[0]['h_url'];
        }
        else
            return false;
    }
    //hàm lấy hình điện thoại

    # Lấy danh sách điện thoại mới
    public static function getNewItems()
    {
        $instant = new Dienthoai();
        $data = $instant->Db->run('SELECT * FROM dienthoai WHERE dt_in_use = 1 ORDER BY dt_last_update DESC LIMIT 0,6');
        foreach ($data as &$row) {
            $row['dt_image_url'] = $instant->getDienthoaiFirstImage($row['dt_id']);


            Dienthoai::format($row);
        }


        return $data;
    }

    public static function delete($phone_id)
    {
        return self::$_database->run('UPDATE dienthoai SET dt_in_use = 0 WHERE dt_id = :id', array('id' => $phone_id));
    }

    # Lấy danh sách điện thoại bán chạy
    public static function getBestSell()
    {
        $sql = <<<SQL
SELECT dt.dt_id, dt_in_use, hsx_in_use, dt_ten, dt_gia, IF (ctddh_soluongdh IS NULL, 0, sum(ctddh_soluongdh)) as soluong
FROM dienthoai dt LEFT JOIN chitietddh ct on dt.dt_id = ct.dt_id
     NATURAL JOIN hangsanxuat
GROUP BY dt.dt_id
HAVING (dt_in_use = 1) AND (hsx_in_use = 1)
ORDER BY soluong DESC
LIMIT 0, 6
SQL;
        $data = self::$_database->run($sql);
        $data = self::format($data, true);
        return $data;
    }

    public function getPhoneImages($id)
    {
        $data = $this->Db->run('SELECT h_url FROM hinhdienthoai WHERE dt_id = :id', array('id' => $id));
        foreach ($data as &$item) {
            $item = $item['h_url'];
        }

        return $data;
    }

    public static function getThumbnail($id)
    {
        $dt = new Dienthoai();
        return $dt->getDienthoaiFirstImage($id);
    }

    # Format điện thoại (giá, ...)
    public static function format(&$dt, $is_table = false)
    {
        if ($is_table) {
            foreach ($dt as &$row) {
                $row = self::_format($row);
            }
        } else {
            $dt = self::_format($dt);
        }

        return $dt;
    }

    public static function getPhoneFormattedInfo($phone_id)
    {
        $info = self::getPhoneInfo($phone_id);
        is_array($info) || $info = array($info);
        return self::_format($info);
    }

    private static function _format($row)
    {
        isset($row['dt_gia']) &&
            (
                ($row['dt_gia_formatted'] = App::formatVnd($row['dt_gia'])) &&
                ($row['dt_gia_short'] = App::formatVndShort($row['dt_gia']))
            );

        isset($row['dt_ten']) &&  isset($row['dt_id']) &&
            (
                ($row['dt_id_seo'] = $row['dt_id'] . '-' . str_replace(' ', '_', $row['dt_ten'])) &&
                ($row['dt_image_url'] = Dienthoai::getThumbnail($row['dt_id']))
            );


        if (isset($row['hsx_id'])) {
            $hsx_ten = Hangsanxuat::getName($row['hsx_id']);
            $row['hsx_id_seo'] = $row['hsx_id'] . '-' . str_replace(' ', '_', $hsx_ten);
            $row['hsx_ten'] = $hsx_ten;
        }

        return $row;
    }

    # Kiểm tra tồn tại mã
    public static function existsId($id)
    {
        return self::getPhoneInfo($id) != false;
    }

    public function sanphamcunggia($price, $id)
    {
        $sql = "SELECT * FROM dienthoai WHERE (dt_in_use = 1) AND dt_gia  BETWEEN :min AND :max AND dt_id <> :id";
        $bind = array();
        $bind['min'] = $price - 500;
        $bind['max'] = $price + 500;
        $bind['id'] = $id;

        return $this->Db->run($sql, $bind);
    }
    //San  pham cung hang
    public function cunghang($hsx_id){
        $sql = "SELECT * FROM dienthoai WHERE (dt_in_use = 1 AND hsx_id = :hsx_id";
        $bind = array();
        $bind['hsx_id'] = $hsx_id;
        return $this->Db->run($sql, $bind);
    }


    public function getComments($id)
    {
        $sql = <<<SQL
SELECT nx_id, dt_id, nx_noidung, DATE_FORMAT(nx_ngay, '%H:%i:%s %d/%m/%Y') AS nx_ngay, nx_hoten, nx_email
FROM nhanxet
WHERE dt_id = :id
ORDER BY nx_ngay DESC
SQL;
        return $this->Db->run($sql, array('id' => $id));
    }

    public function addComment($dt_id, $nx_noidung, $hoten, $email)
    {
        $this->Db->run('INSERT INTO nhanxet(dt_id, nx_noidung, nx_ngay, nx_hoten, nx_email) ' .
            ' VALUES(:dt_id, :noidung, NOW(), :hoten, :email)', array(
            'dt_id' => $dt_id,
            'noidung' => $nx_noidung,
            'hoten' => $hoten,
            'email' => $email
        ));
    }





}